package com.lizhivscaomei.dueros.tobaccoguider.controller;

import com.lizhivscaomei.dueros.tobaccoguider.bot.TobaccoBot;
import com.lizhivscaomei.dueros.tobaccoguider.entity.TobaccoInfo;
import com.lizhivscaomei.dueros.tobaccoguider.repo.TobaccoInfoRepoMysql;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 潍坊烟草应用入口
 *
 * @author lizhi
 */
@Slf4j
@AllArgsConstructor
@RestController
public class IndexController {
    TobaccoInfoRepoMysql tobaccoInfoRepo;

    /**
     * 潍坊烟草应用入口
     */
    @PostMapping("/product/introduce")
    public String introduce(HttpServletRequest request) {
        String msg = "没有找到相关信息";
        try {
            TobaccoBot bot = new TobaccoBot(request);
            msg = bot.run();
            log.info(msg);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 微信小程序获取详情
     *
     * @param boxCode
     * @return
     */
    @GetMapping("/mini/detail")
    public TobaccoInfo findByBoxCode(String boxCode) {
        return tobaccoInfoRepo.findByBoxCode(boxCode);
    }
}
